package com.ruoyi.equipment.service.impl;

import java.util.HashMap;
import java.util.List;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.annotation.DataScope;
import com.ruoyi.common.annotation.Excel;
import com.ruoyi.equipment.domain.*;
import com.ruoyi.equipment.enums.QosEnum;
import com.ruoyi.equipment.enums.TopicEnum;
import com.ruoyi.equipment.mapper.TEquipmentLogMapper;
import com.ruoyi.equipment.mapper.TEquipmentMapper;
import com.ruoyi.equipment.mapper.TToolsMapper;
import com.ruoyi.equipment.util.EmqxUtils;
import net.bytebuddy.implementation.bytecode.Throw;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.equipment.mapper.TDrawerMapper;
import com.ruoyi.equipment.service.ITDrawerService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 仓匣Service业务层处理
 *
 * @author ruoyi
 * @date 2021-12-20
 */
@Service
public class TDrawerServiceImpl implements ITDrawerService {
    @Autowired
    private TDrawerMapper tDrawerMapper;

    @Autowired
    private TEquipmentLogMapper tEquipmentLogMapper;

    @Autowired
    private TEquipmentMapper tEquipmentMapper;

    @Autowired
    private TToolsMapper tToolsMapper;

    /**
     * 查询仓匣
     *
     * @param equipmentId 仓匣主键
     * @return 仓匣
     */
    @Override
    public List<TDrawer> selectTDrawerByEquipmentId(String equipmentId) {
        List<TDrawer> tDrawers = tDrawerMapper.selectTDrawerByEquipmentId(equipmentId);
        return tDrawers;
    }

    /**
     * 查询仓匣列表
     *
     * @param tDrawer 仓匣
     * @return 仓匣
     */
    @Override
    @DataScope(deptAlias="d")
    public List<TDrawer> selectTDrawerList(TDrawer tDrawer) {
        return tDrawerMapper.selectTDrawerList(tDrawer);
    }

    /**
     * 新增仓匣
     *
     * @param tDrawer 仓匣
     * @return 结果
     */
    @Override
    public int insertTDrawer(TDrawer tDrawer) {
        return tDrawerMapper.insertTDrawer(tDrawer);
    }

    /**
     * 修改仓匣
     *
     * @param tDrawer 仓匣
     * @return 结果
     */
    @Override
    public int updateTDrawer(TDrawer tDrawer) {
        Integer toolId = tDrawer.getToolId();
        TTools tTools = tToolsMapper.selectTToolsByToolId(toolId.longValue());
        tDrawer.setDrawerRemark(tTools.getBrand());
        return tDrawerMapper.updateTDrawer(tDrawer);
    }

    /**
     * 批量删除仓匣
     *
     * @param equipmentIds 需要删除的仓匣主键
     * @return 结果
     */
    @Override
    public int deleteTDrawerByEquipmentIds(String[] equipmentIds, Integer[] drawerNos) {
        Integer status = tDrawerMapper.getDrawerDetail(equipmentIds, drawerNos);
        if (status==1){
            return -1;
        }
        return tDrawerMapper.deleteTDrawerByEquipmentIds(equipmentIds, drawerNos);
    }

    /**
     * 删除仓匣信息
     *
     * @param equipmentId 仓匣主键
     * @return 结果
     */
    @Override
    public int deleteTDrawerByEquipmentId(String equipmentId) {
        return tDrawerMapper.deleteTDrawerByEquipmentId(equipmentId);
    }

    /**
     * 获取仓匣的具体信息
     *
     * @param tDrawer
     * @return
     */
    @Override
    public TDrawer selectTDrawerDeatil(TDrawer tDrawer) {

        TDrawer tDrawer1 = tDrawerMapper.selectTDrawerDetail(tDrawer);
        return tDrawer1;
    }

    /**
     * 上线仓匣
     *
     * @param equipmentIds
     * @param drawerNos
     * @return
     */
    @Override
    public int popTDrawerByEquipmentIds(String[] equipmentIds, Integer[] drawerNos) {

        return tDrawerMapper.popTDrawer(equipmentIds, drawerNos);
    }

    /**
     * 借出仓匣操作
     *
     * @param tEquipmentLog
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int lendTDrawer(TEquipmentLog tEquipmentLog) {
        TEquipment tEquipment = tEquipmentMapper.selectTEquipmentByEquipmentId(tEquipmentLog.getEquipmentId());
        if (tEquipment.getStatus()==1){
            throw new RuntimeException("设备不在线");
        }
        tEquipmentLog.setEquipmenName(tEquipment.getEquipmentName());
        tDrawerMapper.lendTDrawer(tEquipmentLog.getEquipmentId(), tEquipmentLog.getDrawerNo());
        int userLogId = tEquipmentLogMapper.insertTEquipmentLog(tEquipmentLog);

        //设备借出向设备发送的消息
        MqttVo mqttVo = new MqttVo();
        mqttVo.setCode(0);
        mqttVo.setDrawerId(tEquipmentLog.getDrawerId());
        mqttVo.setDrawerNo(tEquipmentLog.getDrawerNo().toString());
        mqttVo.setEquipmentId(tEquipmentLog.getEquipmentId());
        mqttVo.setToolId(tEquipmentLog.getToolId());
        mqttVo.setUserLogId(tEquipmentLog.getUserLogId().toString());
        String payload = JSONObject.toJSONString(mqttVo);
        String json= EmqxUtils.publish(tEquipmentLog.getEquipmentId()+"/code", tEquipmentLog.getEquipmentId(), payload, QosEnum.QoS2, false, EmqxUtils.getUrl() + "/api/v4/mqtt/publish");
        JSONObject jsonObject = JSON.parseObject(json);
        Integer code =(Integer) jsonObject.get("code");
        if (code!=0){
           throw new RuntimeException("设备接收消息异常");
        }
        return 1;
    }

}
